
package com.beaut.project.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beaut.project.dao.ExcelMapper;
import com.beaut.project.util.ExcelUtility;
import com.beaut.project.util.StaticData;
import com.beaut.project.util.Utility;

@Transactional(rollbackFor = Exception.class)
@Service("excelService")
public class ExcelService {
	@Autowired
	private ExcelMapper excelMapper;
	public List<Map<String,String>> listLeave(Map<String,Object> map){
		return excelMapper.listLeave(map);
	}
	public List<Map<String,String>> listInventory(Map<String,String> map){
		return excelMapper.listInventory(map);
	}
	public List<Map<String,String>> listFinanceCommonDetail(Map<String, Object> map) {
		return excelMapper.listFinanceCommonDetail(map);
	  }
	public List<Map<String,String>> listMaterialDetail(Map<String,String> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("warehouse_id")).split(","));
		List<Map<String,String>>listM=new ArrayList<Map<String,String>>();
		if(list.size()>0){
			listM= excelMapper.listMaterialDetail(list);
		}
		return listM;
	}
	
	public Map<String,Object> historyUpload(Workbook wb){
		FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
		List<Map<String,Object>> xmList = new ArrayList<Map<String,Object>>();
		int xm_success = XMUpload(evaluator,wb.getSheetAt(0),xmList);
		int gd_success = GDUpload(evaluator,wb.getSheetAt(1),xmList);
		Map<String,Object> rtMap = new HashMap<String,Object>();
		rtMap.put("xm_success", xm_success);
		rtMap.put("gd_success", gd_success);
		return rtMap;
	}
	private int GDUpload(FormulaEvaluator evaluator,Sheet sheet,List<Map<String,Object>> xmList){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		int rowNum = 0;
		for (Row row : sheet) {
			if(rowNum == 0){
				//第1行为表头行，不用读取
				rowNum++;
				continue;
			}
			rowNum++;
			Map<String,Object> map = new HashMap<String,Object>();
			for (int index=0;index<15;index++) {
				CellValue cell = evaluator.evaluate(row.getCell(index));
				switch(index){
				case 0:
					map.put("gd_code", ExcelUtility.getCellValue(cell));
					break;
				case 1:
					map.put("gd_name", ExcelUtility.getCellValue(cell));
					break;
				case 2:
					map.put("gd_admin", ExcelUtility.getCellValue(cell));
					break;
				case 3:
					map.put("gd_worker", ExcelUtility.getCellValue(cell));
					break;
				case 4:
					map.put("gd_worker_admin", ExcelUtility.getCellValue(cell));
					break;
				case 5:
					map.put("gd_start_work_time", ExcelUtility.getCellValue(cell));
					break;
				case 6:
					map.put("gd_assume_end_time", ExcelUtility.getCellValue(cell));
					break;
				case 7:
					map.put("gd_actual_end_time", ExcelUtility.getCellValue(cell));
					break;
				case 8:
					map.put("gd_content", ExcelUtility.getCellValue(cell));
					break;
				case 9:
					map.put("gd_total", ExcelUtility.getCellValue(cell));
					break;
				case 10:
					map.put("xm_name", ExcelUtility.getCellValue(cell));
					break;
				case 11:
					map.put("xm_code", ExcelUtility.getCellValue(cell));
					break;
				case 12:
					map.put("gd_work_money", ExcelUtility.getCellValue(cell));
					break;
				case 13:
					map.put("gd_material_money", ExcelUtility.getCellValue(cell));
					break;
				case 14:
					map.put("gd_other_money", ExcelUtility.getCellValue(cell));
					break;
				}
			}
			for(Map<String,Object> xm_map : xmList){
				if("".equals(String.valueOf(map.get("xm_code")).trim()) && String.valueOf(map.get("xm_name")).trim().equalsIgnoreCase(String.valueOf(xm_map.get("xm_name")).trim())){
					map.put("xm_id", xm_map.get("id"));
				}else if(String.valueOf(map.get("xm_code")).trim().equalsIgnoreCase(String.valueOf(xm_map.get("xm_code")).trim())){
					map.put("xm_id", xm_map.get("id"));
				}
			}
			if(map.get("xm_id") != null){
				list.add(map); 
			}else{
				System.out.println(rowNum+":"+map.get("gd_code")+"-"+map.get("gd_name"));
			}
		}
		if(list.size() > 0){
			excelMapper.uploadGD(list);
		}
		return list.size();
	}
	private int XMUpload(FormulaEvaluator evaluator,Sheet sheet,List<Map<String,Object>> list){
		int rowNum = 0;
		for (Row row : sheet) {
			if(rowNum < 3){
				//前三行为表头行，不用读取
				rowNum++;
				continue;
			}
		   Map<String,Object> map = new HashMap<String,Object>();
		   map.put("id", UUID.randomUUID().toString());
		   for (int index=0;index<28;index++) {
			   CellValue cell = evaluator.evaluate(row.getCell(index));
				   switch(index){
				   case 0:
					   map.put("xm_code", ExcelUtility.getCellValue(cell));
					   break;
				   case 1:
					   map.put("construct_code", ExcelUtility.getCellValue(cell));
					   break;
				   case 2:
					   map.put("xm_name", ExcelUtility.getCellValue(cell));
					   break;
				   case 3:
					   map.put("dept_name", ExcelUtility.getCellValue(cell));
					   break;
				   case 4:
					   map.put("prof_type", ExcelUtility.getCellValue(cell));
					   break;
				   case 5:
					   map.put("prof_detail", ExcelUtility.getCellValue(cell));
					   break;
				   case 6:
					   map.put("customer", ExcelUtility.getCellValue(cell));
					   break;
				   case 7:
					   map.put("customer_dept", ExcelUtility.getCellValue(cell));
					   break;
				   case 8:
					   map.put("customer_admin", ExcelUtility.getCellValue(cell));
					   break;
				   case 9:
					   map.put("xm_content", ExcelUtility.getCellValue(cell));
					   break;
				   case 10:
					   map.put("assume_receive", ExcelUtility.getCellValue(cell));
					   break;
				   case 11:
					   map.put("adjust_factor", ExcelUtility.getCellValue(cell));
					   break;
				   case 12:
					   map.put("already_receive", ExcelUtility.getCellValue(cell));
					   break;
				   case 13:
					   map.put("assume_worker_money_1", ExcelUtility.getCellValue(cell));
					   break;
				   case 14:
					   map.put("assume_worker_money_2", ExcelUtility.getCellValue(cell));
					   break;
				   case 15:
					   map.put("adjust_worker_money_1", ExcelUtility.getCellValue(cell));
					   break;
				   case 16:
					   map.put("adjust_worker_money_2", ExcelUtility.getCellValue(cell));
					   break;
				   case 17:
					   map.put("already_worker_money", ExcelUtility.getCellValue(cell));
					   break;
				   case 18:
					   map.put("assume_material_money", ExcelUtility.getCellValue(cell));
					   break;
				   case 19:
					   map.put("adjust_material_money", ExcelUtility.getCellValue(cell));
					   break;
				   case 20:
					   map.put("already_material_money", ExcelUtility.getCellValue(cell));
					   break;
				   case 21:
					   map.put("assume_other_money", ExcelUtility.getCellValue(cell));
					   break;
				   case 22:
					   map.put("adjust_other_money", ExcelUtility.getCellValue(cell));
					   break;
				   case 23:
					   map.put("already_other_money", ExcelUtility.getCellValue(cell));
					   break;
				   case 24:
					   map.put("manage_money", ExcelUtility.getCellValue(cell));
					   break;
				   case 25:
					   map.put("tax", ExcelUtility.getCellValue(cell));
					   break;
				   case 26:
					   map.put("profit", ExcelUtility.getCellValue(cell));
					   break;
				   case 27:
					   map.put("remark", ExcelUtility.getCellValue(cell));
					   break;
				   }
	       }
		   getRightValue(map);
		   list.add(map); 
		}
		if(list.size() > 0){
			excelMapper.uploadXM(list);
		}
		return list.size();
	}
	private void getRightValue(Map<String,Object> map){
		String manage_money = String.valueOf(map.get("manage_money"));
		String tax = String.valueOf(map.get("tax"));
		String profit = String.valueOf(map.get("profit"));
		if("".equals(manage_money)){
			manage_money = "0";
		}
		if("".equals(tax)){
			tax = "0";
		}
		if("".equals(profit)){
			profit = "0";
		}
		map.put("manage_money",new BigDecimal(manage_money).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
		map.put("tax",new BigDecimal(tax).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
		map.put("profit",new BigDecimal(profit).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
	}
	public List<Map<String,String>> listFinanceCommons1(Map<String,Object> map){
		return excelMapper.listFinanceCommons1(map);
	}
	public List<Map<String,String>> listEmployee(Map<String,Object> map){
		return excelMapper.listEmployee(map);
	}
	
	public List<Map<String,String>> listNewAdvances(Map<String,Object> map){
		return excelMapper.listNewAdvances(map);
	}
	public List<Map<String,String>> listFinanceDeptLog(Map<String,Object> map){
		return excelMapper.listFinanceDeptLog(map);
	}
	
	public List<Map<String,Object>> listFinanceInfo(Map<String,Object> map){
		return excelMapper.listFinanceInfo(map);
	}
	public List<Map<String,String>> listUnPurchaseSupplier(Map<String,Object> map){
		return excelMapper.listUnPurchaseSupplier(map);
	}
	public List<Map<String,String>> hasPurchasedSupplierExcel(Map<String,Object> map){
		return excelMapper.hasPurchasedSupplierExcel(map);
	}
	public List<Map<String,String>> listFianceOutCommons(Map<String,Object> map){
		return excelMapper.listFianceOutCommons(map);
	}
	public List<Map<String,String>> listGdLoan(Map<String,Object> map){
		return excelMapper.listGdLoan(map);
	}
	public List<Map<String,String>> listFinanceGdAdvances(Map<String,Object> map){
		return excelMapper.listFinanceGdAdvances(map);
	}
	public List<Map<String,String>> listGdYetAdvances(Map<String,Object> map){
		return excelMapper.listGdYetAdvances(map);
	}
	public List<Map<String,String>> listXMFinanceFinishEarn(Map<String,Object> map){
		return excelMapper.listXMFinanceFinishEarn(map);
	}
	public List<Map<String,String>> listXMFinanceFinishPayment(Map<String,Object> map){
		return excelMapper.listXMFinanceFinishPayment(map);
	}
	public List<Map<String,String>> listXmReim(Map<String,Object> map){
		return excelMapper.listXmReim(map);
	}
	public List<Map<String,String>> listFianceOutCommons1(Map<String,Object> map){
		return excelMapper.listFianceOutCommons1(map);
	}
	public List<Map<String,Object>> listGDStatisticDetail(Map<String,Object> map){
		List<Map<String,Object>> list= excelMapper.listGDStatisticDetail(map);
		for(int i=0;i<list.size();i++){
			list.get(i).put("gd_status_desc", StaticData.GD_STATUS.get(String.valueOf(list.get(i).get("gd_status"))));
		}
		return list;
	}
	public List<Map<String,Object>> listFinanceCompanyLog(Map<String,Object> map){
		return excelMapper.listFinanceCompanyLog(map);
	}
	public String getFlowRunId(Map<String,String> map){
		return excelMapper.getFlowRunId(map);
	}
	public List<Map<String,String>> listEmployeeLoan(Map<String,Object> map){
		return excelMapper.listEmployeeLoan(map);
	}
	public List<Map<String,String>> listEmployeeReimburseme(Map<String,Object> map){
		return excelMapper.listEmployeeReimburseme(map);
	}
	public List<Map<String,String>> listWorkYetLoan(Map<String,Object> map){
		return excelMapper.listWorkYetLoan(map);
	}
}
